#ifndef __LOADWEBPAGE_H
#define __LOADWEBPAGE_H
#include "Mylogger.hh"
#include "tinyxml2.h"
#include "PagePreprocess/WebPage.h"
#include <string>
#include <map>
#include <unordered_map>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace tinyxml2;
using std::ifstream;
using std::istringstream;
using std::map;
using std::multimap;
using std::pair;
using std::string;
using std::unordered_map;

class LoadWebPage
{
public:
    static LoadWebPage *getInstance();
    bool isDataLoaded() const { return _dataLoaded; }
    bool LodPagefromLib(int id, WebPage &webpage); // 从网页库中加载指定id的网页
private:
    LoadWebPage();
    LoadWebPage(LoadWebPage &) = delete;
    LoadWebPage &operator=(LoadWebPage) = delete;
    ~LoadWebPage() = default;
    void LodoffsetLib(const string &path, map<int, pair<int, int>> &off_set); // 加载网页偏移库
    void LodinvertIndexLib();                                                 // 加载倒排索引库
    bool _dataLoaded = false;
    class AutoRelease
    {
    public:
        AutoRelease() {};
        ~AutoRelease()
        {
            if (_pInstance)
            {
                LogInfo("释放网页库");
                delete _pInstance;
                _pInstance = nullptr;
            }
        }
    };

public:
    map<int, pair<int, int>> _newoffset;                     // 存放新的网页偏移库id,偏移量，长度
    unordered_map<string, map<int, double>> _invertIndexLib; // 倒排索引库，单词，id，权重
private:
    //XMLDocument _xmldoc;
    static LoadWebPage *_pInstance; // 单例对象
    static AutoRelease _ar;
};

#endif